package com.cloud.system.project.controller;

import com.cloud.common.core.enums.AuthEnums;
import com.cloud.common.core.model.response.ApiResponses;
import com.cloud.common.core.model.supers.SuperController;
import com.cloud.common.security.annotation.PreAuth;
import com.cloud.system.project.model.entity.User;
import com.cloud.system.project.service.ITestService;
import com.cloud.system.project.service.IUserService;
import com.cloud.system.utils.excel.ExcelData;
import com.cloud.system.utils.excel.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 测试接口 前端控制器
 * </p>
 *
 * @author Sam
 * @since 2020-12-27
 */
@Api(tags = {"测试接口"})
@RestController
@RequestMapping("/open/test")
public class TestRestController extends SuperController {

    @Value("${server.port}")
    private String port;
    @Autowired
    private IUserService userService;
    @Autowired
    private ITestService testService;

    @PreAuth(AuthEnums.OPEN)
    @ApiOperation("测试负载Feign")
    @GetMapping("/load")
    public ApiResponses<String> load() {
        return success(port);
    }

    @PreAuth(AuthEnums.OPEN)
    @ApiOperation("测试秒杀1")
    @PostMapping("/secKill1")
    public ApiResponses<Void> secKill1() {
        userService.secKill("1002");
        return success();
    }

    @PreAuth(AuthEnums.OPEN)
    @ApiOperation("测试秒杀2")
    @PostMapping("/secKill2")
    public ApiResponses<Void> secKill2() {
        userService.secKill(new User().setId("1002"));
        return success();
    }

    @PreAuth(AuthEnums.OPEN)
    @ApiOperation("测试秒杀入口")
    @PostMapping("/secKillStart")
    public ApiResponses<Void> secKillStart() {
        for (int i = 0; i < 500; i++) {
            this.secKill1();
        }
        return success();
    }

    @PreAuth(AuthEnums.OPEN)
    @ApiOperation("分布式事务测试")
    @PostMapping("/globalTranTest")
    public ApiResponses<Void> globalTranTest() {
        testService.globalTranTest();
        return success();
    }

    @PreAuth(AuthEnums.OPEN)
    @ApiOperation("导入excel解析")
    @PostMapping("/importExcel")
    public ApiResponses<List<ExcelData>> importExcel(MultipartFile file) {
        List<ExcelData> dataList = ExcelUtils.importExcelAll(file);
        return success(dataList);
    }

}

